import java.io.FileOutputStream;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
//将java bean转成xml
public class JaxbMarshal {

	public static void main(String[] args) {
		try {
			Book2 book = new Book2();
			book.setId("1");
			book.setAuthor("千年老二");
			book.setBookname("葵花宝典");
			book.setPrice("1234");
			JAXBContext context = JAXBContext.newInstance(Book2.class);
			Marshaller m = context.createMarshaller();
			StringWriter sw = new StringWriter();
			m.marshal(book, sw);
			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);// 是否格式化
			m.marshal(book, new FileOutputStream("src/main/java/book.xml"));
			System.out.println(sw.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
